package com.yiqi.lottery.feature.mine.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.BeetingBo;
import com.yiqi.lottery.entity.bo.LotteryBo;
import com.yiqi.lottery.entity.bo.OrderBo;
import com.yiqi.lottery.entity.bo.OrderListBo;
import com.yiqi.lottery.feature.documentary.view.adapter.BeetingListAdapter;
import com.yiqi.lottery.feature.documentary.view.adapter.LotteryListAdapter;
import com.yiqi.lottery.feature.lottery.dependencies.order.DaggerOrderComponent;
import com.yiqi.lottery.feature.lottery.dependencies.order.OrderPresenterModule;
import com.yiqi.lottery.feature.lottery.view.BuyHallDetailActivity;
import com.yiqi.lottery.feature.mine.contract.OrderContract;
import retrofit2.http.POST;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;


public class OrderListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, OrderContract.View, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener, BaseQuickAdapter.OnItemChildClickListener {

    BeetingListAdapter mAdapter;
    @BindView(R.id.rv_lottery_list)
    RecyclerView mRvBeeting;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private EmptyView mEmptyView;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int pageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private int mPid;
    private Integer mOrderType = 1;

    public static final OrderListFragment newInstance(int pid, String message) {
        OrderListFragment fragment = new OrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("pid", pid);
        bundle.putString("message", message);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Inject
    OrderContract.Presenter mPresenter;
    private PageData<OrderListBo> mData;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_beeting_list;
    }

    @Override
    protected void initInjector() {
        DaggerOrderComponent.builder().orderPresenterModule(new OrderPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initAdapter(mOrderType);
    }

    @Override
    protected void initData() {
        if (mOrderType == 9) {
            if (mPresenter != null)
                mPresenter.getMyCopurceOrderList(pageIndex);
        } else {
            if (mPresenter != null)
                mPresenter.getOrderList(pageIndex, mOrderType);
        }

    }

    @Override
    public void onStart() {
        super.onStart();

        switch (mPid) {

            case 1:
                mOrderType = 1;
                break;
            case 9:
                mOrderType = 9;
                break;
            case 2:
                mOrderType = 2;
                break;
            case 3:
                mOrderType = 3;
                break;
        }

        initAdapter(mOrderType);
        initData();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPid = getArguments().getInt("pid");
    }

    private void initAdapter(int type) {
        mAdapter = new BeetingListAdapter(type);
        mEmptyView = new EmptyView(mRvBeeting);
        mRvBeeting.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvBeeting.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(11));
        mAdapter.bindToRecyclerView(mRvBeeting);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


        if (mOrderType == 9) {
            Intent intentw = new Intent(getActivity(), BuyHallDetailActivity.class);
            intentw.putExtra("id", mData.getList().get(position).getCopurce_id());
            intentw.putExtra("type", false);
            startActivity(intentw);
        } else {
            Intent intent = new Intent(getActivity(), OrderInfoActivity.class);
            intent.putExtra("id", mData.getList().get(position).getOrder_id());
            intent.putExtra("type", mOrderType);
            startActivity(intent);
        }

    }

    @Override
    public void showOrderListSuccess(PageData<OrderListBo> msg) {
        mRefreshLayout.finishRefresh();
        initData();
        mData = msg;
        mAdapter.setNewData(mData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);


    }

    @Override
    public void showOrderListError(String msg) {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(msg));
        mAdapter.setNewData(null);
    }

    @Override
    public void showOrderListMoreSuccess(PageData<OrderListBo> msg) {
        isErr = false;
        mAdapter.addData(msg.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showOrderListMoreError(String msg) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showOrderListEmpty() {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无订单数据！"));
        mAdapter.setNewData(null);
    }

    @Override
    public void onLoadMoreRequested() {
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {

            if (mCurrentCounter >= mData.getTotal()) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    pageIndex++;
                    if (mOrderType == 9) {
                        if (mPresenter != null)
                            mPresenter.getMyCopurceOrdeMorerList(pageIndex);
                    } else {
                        if (mPresenter != null)
                            mPresenter.getOrderMoreList(pageIndex, mOrderType);
                    }

                }
            }
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageIndex = 0;
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            initData();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
       Intent intent=new Intent(getActivity(),BillingActivity.class);
       intent.putExtra("info",mAdapter.getData().get(position));
       startActivity(intent);
    }
}
